<?php


namespace app\controller\equipment;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * PC后端设备管理
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取设备列表
     */
	public function api_equipment_index_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'category_name' => isset($msg['data']['category_name'])?$msg['data']['category_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'merchant_circle_name' => isset($msg['data']['merchant_circle_name'])?$msg['data']['merchant_circle_name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'merchant_circle_id' => isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['category_id'])){
            $where .= " AND category_id =".$info['category_id'];
        }
        if(!empty($info['merchant_circle_id'])){
            $where .= " AND merchant_circle_id in (".$info['merchant_circle_id'].")";
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['code'])){
            $where .= " AND code LIKE '%".$info['code']."%'";
        }
        if(!empty($info['attribute'])){
            $where .= " AND attribute LIKE '%".$info['attribute']."%'";
        }
        if(!empty($info['category_name'])){
            $where .= " AND category_name LIKE '%".$info['category_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['merchant_circle_name'])){
            $where .= " AND merchant_circle_name LIKE '%".$info['merchant_circle_name']."%'";
        }
        if(!empty($info['linkman'])){
            $where .= " AND linkman LIKE '%".$info['linkman']."%'";
        }
        if(!empty($info['linkman_mobile'])){
            $where .= " AND linkman_mobile LIKE '%".$info['linkman_mobile']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
		$list = $db->query("select * from equipment_list where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from equipment_list WHERE ".$where);
        if($info['type'] == 1){
            $merchant = $this->table->find('merchant_expand',['id'=>$list[0]['merchant_id']]);
            $list[0]['merchant_linkman'] = $merchant['linkman'];
            $list[0]['merchant_linkman_mobile'] = $merchant['mobile'];
            $admin_region = $db->query("SELECT name FROM admin_region WHERE region_ids in (".$merchant['province_id'].")");
            $list[0]['merchant_region_name'] = $admin_region[0]['name'];
        }
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 编辑
     */
    public function api_equipment_index_edit($msg)
    {   
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'address' => isset($msg['data']['address'])?$msg['data']['address']:'',
            'merchant_circle_id' => isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'merchant_circle_name' => isset($msg['data']['merchant_circle_name'])?$msg['data']['merchant_circle_name']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $data = [
            'address' => $info['address'],
            'merchant_circle_id' => $info['merchant_circle_id'],
            'merchant_circle_name' => $info['merchant_circle_name'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('equipment_list',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }



    /**
     * 删除
     */
    public function api_equipment_index_delete($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        $res = $this->table->delete('equipment_list',['id'=>$id]);
        if(!$res){
            return $this->outMessage(-1,'删除失败');
        }
        return $this->outMessage(10000,'删除成功');
    }



    /**
     * 设备申请
     */
    public function api_equipment_index_add($msg)
    {   
        if($this->login_type !== 3){
            return $this->outMessage(-1,'只有商家才可申请设备');
        }
        $info = [
            'linkman' =>  isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
            'address' => isset($msg['data']['address'])?$msg['data']['address']:'',
            'number' => isset($msg['data']['number'])?$msg['data']['number']:'',
            'equipment_id' => isset($msg['data']['equipment_id'])?$msg['data']['equipment_id']:'',
            'merchant_circle_id' =>  isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'merchant_circle_name' => isset($msg['data']['merchant_circle_name'])?$msg['data']['merchant_circle_name']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['assess_person_number'] =  isset($msg['data']['assess_person_number'])?$msg['data']['assess_person_number']:0;
        $info['assess_turnover'] =  isset($msg['data']['assess_turnover'])?$msg['data']['assess_turnover']:0;
        $info['remark'] =  isset($msg['data']['remark'])?$msg['data']['remark']:'';
        if(!$this->validate->checkName($info['linkman'])){
            return $this->outMessage(-1,'请输入正确的收货人名称');
        }
        if(!$this->validate->checkMobile($info['linkman_mobile'])){
            return $this->outMessage(-1,'请输入正确的联系电话');
        }
        if(!$this->validate->checkName($info['address'],2,100)){
            return $this->outMessage(-1,'请输入正确的收货地址');
        }
        if(!is_numeric($info['number']) || $info['number'] < 1 || $info['number'] > 10000){
            return $this->outMessage(-1,'请输入合理的申请数量');
        }
        $merchant = $this->table->find('merchant_expand',['id'=>$this->admin_id]);
        $equipment = $this->table->find('equipment',['id'=>$info['equipment_id']]); 
        $trade = $this->table->find('trade',['id'=>$merchant['main_category_id']]);
        $supplier = $this->table->find('supplier',['id'=>$equipment['supplier_id']]);
        $itoc = $this->table->find('itoc',['region_id'=>$merchant['city_id']]);
        if(empty($itoc)){
            return $this->outMessage(-1,'该地区暂无运营中心不可申请');
        }
        $data = [
            'order_no' => $this->common->createOutTradeNo('S'),
            'linkman' => $info['linkman'],
            'linkman_mobile' => $info['linkman_mobile'],
            'number' => $info['number'],
            'equipment_id' => $info['equipment_id'],
            'equipment_name' => $equipment['name'],
            'attribute' => $equipment['attribute'],
            'brand_id' => $equipment['brand_id'],
            'brand_name' => $equipment['brand_name'],
            'price' => $equipment['price'],
            'sum_price' => $equipment['price']*$info['number'],
            'merchant_id' => $this->admin_id,
            'merchant_name' => $merchant['name'],
            'trade_id' => $merchant['main_category_id'],
            'trade_name' => $trade['name'],
            'region_id' => $merchant['region_id'],
            'region_name' => $merchant['region_name'],
            'merchant_circle_id' => $info['merchant_circle_id'],
            'merchant_circle_name' => $info['merchant_circle_name'],
            'address' => $info['address'],
            'category_id' => $equipment['category_id'],
            'category_name' => $equipment['category_name'],
            'assess_person_number' => $info['assess_person_number'],
            'assess_turnover' => $info['assess_turnover'],
            'remark' => $info['remark'],
            'itoc_id' => $itoc['id'],
            'itoc_name' => $itoc['name'],
            'supplier_id' => $equipment['supplier_id'],
            'supplier_name' => $equipment['supplier_name'],
            'supplier_linkman' => $supplier['linkman'],
            'supplier_linkman_mobile' => $supplier['linkman_mobile'],
            'supplier_address' => $supplier['region_name'].$supplier['address'],
            'assess_person_number' => $info['assess_person_number'],
            'assess_turnover' => $info['assess_turnover'],
            'remark' => $info['remark'],
            'request_nicname' => $merchant['linkman'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('equipment_request',$data);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        return $this->outMessage(10000,'提交成功,请耐心等待审核.');
    }



    /**
     * 获取设备申请列表
     */
    public function api_equipment_index_getRequestList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'equipment_name' => isset($msg['data']['equipment_name'])?$msg['data']['equipment_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:''
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['equipment_name'])){
            $where .= " AND equipment_name LIKE '%".$info['equipment_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND supplier_name LIKE '%".$info['supplier_name']."%'";
        }
        $list = $db->query("select * from equipment_request where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from equipment_request WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }



    /**
     * 审核
     */
    public function api_equipment_index_requestCheck($msg)
    {
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['check_remark'] = isset($msg['data']['check_remark'])?$msg['data']['check_remark']:'';
        if(!empty($info['check_remark'])){
            if(!$this->validate->checkName($info['check_remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的审核备注');
            }
        }
        $request = $this->table->find('equipment_request',['id'=>$info['id']]);
        if(empty($request)){
            return $this->outMessage(-1,'该设备申请不存在');
        }
        if($request['status'] == $info['status']){
            return $this->outMessage(-1,'不可重复审核');
        }
        if($info['status'] < $request['status']){
            return $this->outMessage(-1,'不可进行该操作');
        }
        $data = [
            'status' => $info['status'],
            'check_admin_id' => $this->admin_id,
            'check_time' => date('Y-m-d H:i:s',time()),
            'check_remark' => $info['check_remark'],
            'update_time' => date('Y-m-d H:i:s',time()),
        ];
        $res = $this->table->update('equipment_request',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'审核失败');
        }
        return $this->outMessage(10000,'审核成功');
    }




    /**
     * 安装完成
     */
    public function api_equipment_index_requestStatusEdit($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'id不能为空');
        }
        $info = $this->table->find('equipment_request',['id'=>$id]);
        if($info['status'] != 4){
            return $this->outMessage(-1,'该设备申请状态暂不能安装完成');
        }
        $res = $this->table->update('equipment_request',['id'=>$id],['status' => 5]);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        $res = $this->table->update('hq_equipment_order_list',['request_order_no'=>$info['order_no']],['status' => 4]);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        $order_no = $this->table->find('hq_equipment_order_list',['request_order_no'=>$info['order_no']]);
        $list = $this->table->select('hq_equipment_order_list',['order_no'=>$order_no['order_no']],'status');
        $status = 0;
        foreach ($list as $v) {
            if($v['status'] != 4){
                $status = 1;
            }
        }
        if($status == 0){
            $res = $this->table->update('hq_equipment_order',['order_no'=>$order_no['order_no']],['status' => 4]);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        return $this->outMessage(10000,'提交成功');
    }




    /**
     * 生成总部设备采购单
     */
    public function api_equipment_index_createEquipmentOrder($msg)
    {   
        global $db;
        $ids = isset($msg['data']['ids'])?$msg['data']['ids']:'';
        if(empty($ids)){
            return $this->outMessage(-1,'id不能为空');
        }
        $request_list = $db->query("SELECT * FROM equipment_request WHERE id in ($ids)");
        if(empty($request_list)){
            return $this->outMessage(-1,'请选择');
        }
        foreach ($request_list as $v) {
            if($v['status'] != 2){
                return $this->outMessage(-1,'请选择待采购的申请');
            }
        }
        $supplier= [];
        $arr = [];
        foreach ($request_list as $k => $v) {
            if(!in_array($v['supplier_id'],$supplier)){
                array_push($supplier,$v['supplier_id']);
                $arr[$v['supplier_id']]['order_no'] = $this->common->createOutTradeNo('HQ');
                $arr[$v['supplier_id']]['number'] = $v['number'];
                $arr[$v['supplier_id']]['price'] = $v['price'];
                $arr[$v['supplier_id']]['money'] = $v['sum_price'];
                $arr[$v['supplier_id']]['supplier_id'] = $v['supplier_id'];
                $arr[$v['supplier_id']]['supplier_name'] = $v['supplier_name'];
                $arr[$v['supplier_id']]['supplier_linkman'] = $v['supplier_linkman'];
                $arr[$v['supplier_id']]['supplier_linkman_mobile'] = $v['supplier_linkman_mobile'];
                $arr[$v['supplier_id']]['supplier_address'] = $v['supplier_address'];
                $arr[$v['supplier_id']]['create_time'] = date('Y-m-d H:i:s',time());
                $arr[$v['supplier_id']]['is_delete'] = 1;
            }else{
                $arr[$v['supplier_id']]['number'] += $v['number'];
                $arr[$v['supplier_id']]['price'] += $v['price'];
                $arr[$v['supplier_id']]['money'] += $v['sum_price'];
            }
            $data = [
               'order_no' => $arr[$v['supplier_id']]['order_no'],
               'request_order_no' => $v['order_no'],
               'linkman' => $v['linkman'],
               'linkman_mobile' => $v['linkman_mobile'],
               'equipment_id' => $v['equipment_id'],
               'equipment_name' => $v['equipment_name'],
               'brand_id' => $v['brand_id'],
               'brand_name' => $v['brand_name'],
               'category_id' => $v['category_id'],
               'category_name' => $v['category_name'],
               'attribute' => $v['attribute'],
               'number' => $v['number'],
               'price' => $v['price'],
               'sum_price' => $v['sum_price'],
               'itoc_id' => $v['itoc_id'],
               'itoc_name' => $v['itoc_name'],
               'merchant_id' => $v['merchant_id'],
               'merchant_name' => $v['merchant_name'],
               'region_id' => $v['region_id'],
               'region_name' => $v['region_name'],
               'address' => $v['address'],
               'create_time' => date('Y-m-d H:i:s',time()),
               'is_delete' => 1,
            ];
            $res = $this->table->insert('hq_equipment_order_list',$data);
            if(empty($res)){
                return $this->outMessage( -2,'采购单生成失败');
            }
        }
        $order = [];
        foreach ($arr as $v) {
            $res = $this->table->insert('hq_equipment_order',$v);
            if(empty($res)){
                return $this->outMessage(-1,'采购单生成失败');
            }
            array_push($order,$res);
        }
        $order_ids = implode(',',$order);
        $list = $db->query("SELECT * FROM hq_equipment_order WHERE id in ($order_ids)");
        foreach ($list as $k => $v) {
            $list[$k]['list'] =  $this->table->select('hq_equipment_order_list',['order_no'=>$v['order_no']]);
        }
        return $this->outMessage(10000,'生成成功',$list);
    }



    /**
     * 采购单提交
     */
    public function api_equipment_index_orderSubmit($msg)
    {   
        global $db;
        $data = isset($msg['data'])?$msg['data']:'';
        if(empty($data) || !is_array($data)){
            return $this->outMessage(-1,'不能为空');
        }
        $user = $this->table->find('admin',['id'=>$this->admin_id]);
        foreach ($data as $v) {
            $res = $db->query("UPDATE hq_equipment_order SET is_delete = 0,remark = '".$v['remark']."' WHERE id = ".$v['id']." AND orders_time = '".date('Y-m-d H:i:s',time())."' AND orders_admin_id = ".$this->admin_id." AND orders_nickname = '".$user['nickname']."'");
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        $ids =  implode(',', array_column($data,'id')); 
        $list = $db->query("SELECT `order_no` FROM hq_equipment_order WHERE id in ($ids)");
        foreach ($list as $v) {
            $res = $db->query("UPDATE hq_equipment_order_list SET is_delete = 0 WHERE order_no ='".$v['order_no']."' ");
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }  
        return $this->outMessage(10000,'提交成功');
    }



    /**
     * 获取采购订单列表
     */
    public function api_equipment_index_orderList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND supplier_name LIKE '%".$info['supplier_name']."%'";
        }
        $list = $db->query("select * from hq_equipment_order where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from hq_equipment_order WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }



    /**
     * 获取采购订单详情
     */
    public function api_equipment_index_orderDetails($msg)
    {   
        $order_no = isset($msg['data']['order_no'])?$msg['data']['order_no']:'';
        if(empty($order_no)){
            return $this->outMessage(-1,'采购单编号不能为空');
        }
        $list = $this->table->select('hq_equipment_order_list',['order_no'=>$order_no]);
        return $this->outMessage(10000,'获取成功',$list,'success',count($list));
    }



    /**
     * 采购单审核
     */
    public function api_equipment_index_orderCheck($msg)
    {   
        global $db;
        $info = [
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'check_remark' => isset($msg['data']['check_remark'])?$msg['data']['check_remark']:'',
        ];
        if(empty($info['order_no'])){
            return $this->outMessage(-1,'采购单编号不能为空');
        }
        if(!is_numeric($info['status'])){
            return $this->outMessage( -2,'审核失败');
        }
        $user = $this->table->find('admin',['id'=>$this->admin_id]);
        $data = [
            'status' => $info['status'],
            'check_time' => date('Y-m-d H:i:s',time()),
            'check_admin_id' => $this->admin_id,
            'check_admin_nickname' => $user['nickname']
        ];
        if(!empty($info['check_remark'])){
           $data['check_remark'] = $info['check_remark'];
        }
        $res = $this->table->update('hq_equipment_order',['order_no'=>$info['order_no']],$data);
        if(!$res){
            return $this->outMessage(-1,'审核失败');
        }
        if($info['status'] == 2){
            $this->table->update('hq_equipment_order_list',['order_no'=>$info['order_no']],['status'=>2]);
            //更改申请状态
            $list = $this->table->select('hq_equipment_order_list',['order_no'=>$info['order_no']],"request_order_no",1,100);
            foreach ($list as $v) {
                $res = $db->query("UPDATE equipment_request SET status = 3,buy_time = '".date('Y-m-d H:i:s',time())."' WHERE order_no ='".$v['request_order_no']."'");
            }
        }
        return $this->outMessage(10000,'审核成功');
    }



    /**
     * 采购单发货/填写物流信息
     */
    public function api_equipment_index_orderSend($msg)
    {
        $request_order_no = isset($msg['data']['request_order_no'])?$msg['data']['request_order_no']:'';
        $order_number = isset($msg['data']['order_number'])?$msg['data']['order_number']:'';
        if(empty($request_order_no)){
            return $this->outMessage(-1,'订单号不能为空');
        }
        if(empty($order_number)){
            return $this->outMessage(-1,'物流单号不能为空');
        }
        $info = $this->table->find('hq_equipment_order_list',['request_order_no'=>$request_order_no]);
        if($info['status'] != 2){
            return $this->outMessage(-1,'该订单状态暂不可发货');
        }
        $data = [
            'status' => 3,
            'order_number' => $order_number,
            'logistics_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('hq_equipment_order_list',['request_order_no'=>$request_order_no],$data);
        if(!$res){
            return $this->outMessage(-1,'发货失败');
        }
        //申请订单修改状态
        $data['status'] = 4;
        $res = $this->table->update('equipment_request',['order_no'=>$request_order_no],$data);
        if(!$res){
            return $this->outMessage(-1,'发货失败');
        }
        //如果全部发货完成则修改采购单状态
        $list = $this->table->select('hq_equipment_order_list',['order_no'=>$info['order_no']],'status');
        $status = 0;
        foreach ($list as $v) {
            if($v['status'] == 1 || $v['status'] == 2){
                $status = 1;
            }
        }
        if($status == 0){
            $res = $this->table->update('hq_equipment_order',['order_no'=>$info['order_no']],['status' => 3]);
            if(!$res){
                return $this->outMessage(-1,'发货失败');
            }
        }
        return $this->outMessage(10000,'发货成功');
    }



    /**
     * 终端商品报表
     */
    public function api_equipment_index_equipmentGoodsList($msg)
    {
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'merchant_name' => isset($msg['data']['merchant_name'])?$msg['data']['merchant_name']:'',
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'equipment_id' => isset($msg['data']['equipment_id'])?$msg['data']['equipment_id']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "a.is_delete = 0";
        if(!empty($info['itoc_name'])){
            $where .= " AND b.itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['merchant_name'])){
            $where .= " AND b.merchant_name LIKE '%".$info['merchant_name']."%'";
        }
        if(!empty($info['code'])){
            $where .= " AND a.equipment_code LIKE '%".$info['code']."%'";
        }
        if(!empty($info['equipment_id'])){
            $where .= " AND a.equipment_id in (".$info['equipment_id'].") ";
        }
        if(empty($info['type'])){
            $list = $db->query("SELECT sum(a.sum_number) as sum_number,sum(a.surplus_number) as surplus_number,b.itoc_name,b.merchant_name,b.category_name,a.equipment_code,a.goods_code,a.goods_name,a.price,a.attribute from equipment_goods as a left join equipment_list as b on a.equipment_code = b.code WHERE ".$where." GROUP BY a.equipment_code,a.goods_id order by a.id  desc limit ".$limit); 
        }else{
            $list = $db->query("SELECT c.id,c.name,c.code,c.brand_name,c.attribute,c.units_name,c.price from equipment_goods as a left join equipment_list as b on a.equipment_code = b.code left join goods as c on a.goods_id = c.id WHERE ".$where." GROUP BY a.goods_id order by a.id  desc limit ".$limit); 
        }
        $count =  $db->query("SELECT * from equipment_goods as a left join equipment_list as b on a.equipment_code = b.code WHERE ".$where." GROUP BY a.goods_id");
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));
    }



    /**
     * 终端数量统计
     */
    public function api_equipment_index_numberStat($msg)
    {
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'admin_region_id' => isset($msg['data']['admin_region_id'])?$msg['data']['admin_region_id']:'',
            'itoc_id' => isset($msg['data']['itoc_id'])?$msg['data']['itoc_id']:'',
            'merchant_circle_id' => isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['itoc_id'])){
            $where .= " AND itoc_id =".$info['itoc_id'];
        }
        if(!empty($info['merchant_circle_id'])){
            $where .= " AND merchant_circle_id in (".$info['merchant_circle_id'].") ";
        }
        if(!empty($info['category_id'])){
            $where .= " AND category_id in (".$info['category_id'].") ";
        }
        //如果存在大区筛选
        if(!empty($info['admin_region_id'])){
            $region_id = $db->query("SELECT region_ids FROM admin_region WHERE id in (".$info['admin_region_id'].")");
            $ids = '';
            foreach ($region_id as $k => $v) {
                if(empty($ids)){
                    $ids .= $v['region_ids'];
                }else{
                    $ids .= ','.$v['region_ids'];
                }
            }
            $region_ids = $this->table->getRegionIds($ids);
            if($region_ids != 'all' && !empty($region_ids)){
                $where .= " AND region_id in (".$region_ids.") ";
            }
        }
        $list = $db->query("SELECT region_id,itoc_id,itoc_name,merchant_circle_id,merchant_circle_name,category_id,category_name,count(*) as count FROM equipment_list WHERE ".$where." GROUP BY itoc_id,merchant_circle_id,category_id limit ".$limit);
        $count =  $db->query("SELECT itoc_id,itoc_name,merchant_circle_id,merchant_circle_name,category_id,category_name,count(*) as count FROM equipment_list WHERE ".$where." GROUP BY itoc_id,merchant_circle_id,category_id");
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $id = substr($v['region_id'],0,2).'0000';
                $admin_region_info = $db->query("SELECT name FROM admin_region WHERE region_ids in (".$id.")");
                var_dump($admin_region_info);
                $list[$k]['admin_region_name'] = $admin_region_info[0]['name'];
            }
        }
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));
    }


    
    /**
     * 终端数量统计查看详情
     */
    public function api_equipment_index_numberStatDetails($msg)
    {
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'itoc_id' => isset($msg['data']['itoc_id'])?$msg['data']['itoc_id']:'',
            'merchant_circle_id' => isset($msg['data']['merchant_circle_id'])?$msg['data']['merchant_circle_id']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['itoc_id'])){
            $where .= " AND itoc_id =".$info['itoc_id'];
        }
        if(!empty($info['merchant_circle_id'])){
            $where .= " AND merchant_circle_id =".$info['merchant_circle_id'];
        }
        if(!empty($info['category_id'])){
            $where .= " AND category_id =".$info['category_id'];
        }
        $list = $db->query("SELECT code FROM equipment_list WHERE ".$where." limit ".$limit);
        $count =  $db->query("SELECT count(*) as count FROM equipment_list WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));
    }

}


